package com.xzq.erp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzq.erp.domain.dto.ComplaintReplyDTO;
import com.xzq.erp.domain.dto.PageDTO;
import com.xzq.erp.domain.po.ComplaintReply;
import com.xzq.erp.domain.po.Customer;
import com.xzq.erp.domain.query.ComplaintReplyPageQuery;
import com.xzq.erp.domain.vo.ComplaintReplyVO;
import com.xzq.erp.enums.ReplyStatus;
import com.xzq.erp.mapper.ComplaintReplyMapper;
import com.xzq.erp.service.IComplaintReplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 投诉回复表 服务实现类
 * </p>
 *
 * @author xzq
 * @since 2025-05-13
 */
@Service
@Slf4j
public class ComplaintReplyServiceImpl extends ServiceImpl<ComplaintReplyMapper, ComplaintReply> implements IComplaintReplyService {

    @Override
    public PageDTO<ComplaintReplyVO> getComplaintReplyList(ComplaintReplyPageQuery query) {
        PageHelper.startPage(query.getCurrent(), query.getSize());
        // 注意Page继承了ArrayList，所以它是个集合对象
        Page<ComplaintReplyVO> page = this.baseMapper.getComplaintReplyList(query);// 查询分页查询列表
        List<ComplaintReplyVO> result = page.getResult();
        if(CollectionUtils.isEmpty(result)){
            return PageDTO.empty(page.getTotal(), (long)page.getPages());
        }
        log.info("查询结果:{},{}", page.getPages(), page.getTotal());
        return PageDTO.of(page);
    }

    @Override
    public List<ComplaintReplyVO> getReplyListByAfterSalesId(Long afterSalesId) {
        List<ComplaintReplyVO> replyList = this.baseMapper.getReplyListByAfterSalesId(afterSalesId);
        if (CollectionUtils.isEmpty(replyList)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(replyList, ComplaintReplyVO.class);
    }

    @Override
    public void addComplaintReply(ComplaintReplyDTO replyDTO) {
        ComplaintReply reply = BeanUtil.copyProperties(replyDTO, ComplaintReply.class);
        if(Objects.nonNull(replyDTO.getSatisfactionLevel()) && replyDTO.getSatisfactionLevel() != 0 && (replyDTO.getSatisfactionLevel() < 1 || replyDTO.getSatisfactionLevel() > 5)){
            throw new IllegalArgumentException("满意度评分必须在1-5分之间");
        }
        reply.setReplyTime(LocalDateTime.now());
        reply.setCreateTime(LocalDateTime.now());
        reply.setUpdateTime(LocalDateTime.now());
        save(reply);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateComplaintReply(ComplaintReplyDTO replyDTO) {
        if (replyDTO.getId() == null) {
            throw new IllegalArgumentException("回复ID不能为空");
        }
        if(Objects.nonNull(replyDTO.getSatisfactionLevel()) && replyDTO.getSatisfactionLevel() != 0 && (replyDTO.getSatisfactionLevel() < 1 || replyDTO.getSatisfactionLevel() > 5)){
            throw new IllegalArgumentException("满意度评分必须在1-5分之间");
        }
        ComplaintReply reply = getById(replyDTO.getId());
        if (reply == null) {
            throw new IllegalArgumentException("回复不存在");
        }
        
        BeanUtil.copyProperties(replyDTO, reply, "id", "createTime");
        reply.setUpdateTime(LocalDateTime.now());
        updateById(reply);
    }

    @Override
    public void deleteComplaintReply(Long id) {
        removeById(id);
    }

    @Override
    public void submitSatisfactionRating(Long id, Integer rating) {
        if (rating == null || rating < 1 || rating > 5) {
            throw new IllegalArgumentException("满意度评分必须在1-5分之间");
        }
        
        ComplaintReply reply = getById(id);
        if (reply == null) {
            throw new IllegalArgumentException("回复不存在");
        }
        
        reply.setSatisfactionLevel(rating);
        reply.setUpdateTime(LocalDateTime.now());
        updateById(reply);
    }

    @Override
    public List<ComplaintReplyVO> getAllComplaintReplies() {
        List<ComplaintReplyVO> replyList = this.baseMapper.getAllComplaintReplies();
        if (CollectionUtils.isEmpty(replyList)) {
            return Collections.emptyList();
        }
        return replyList;
    }
}
